<template>
  <section class="timeline-container timeline">
    <div @click="changeCurrentTimePoint(point)" v-for="point in points" class="timeline-block">
      <div class="timeline-img" :class="['timeline-img',point.pointColor===undefined?'red':point.pointColor]">
        <img v-if="point.img" :src="getImg(point.img)" alt="">
      </div>

      <div class="timeline-content">
        <div class="f16 mb02" v-text="point.title"></div>
        <div class="f14 f-color-grey" v-text="point.text"></div>
        <a :href="point.linkUrl"
           class="read-more"
           target="_blank"
           v-if="point.linkUrl"
           v-html="point.linkText!=undefined?point.linkText:'Read more'"
        ></a>
        <div v-if="point.date" class="f12 f-color-grey mt5" v-text="point.date"></div>
      </div>
    </div>
  </section>
</template>
<style scoped>
  .timeline-container {
    /* this class is used to give a max-width to the element it is applied to, and center it horizontally when it reaches that max-width */
    width: 90%;
    max-width: 1170px;
    margin: 0 auto;
  }

  .timeline-container::after {
    /* clearfix */
    content: '';
    display: table;
    clear: both;
  }

  /* --------------------------------

  Main components

  -------------------------------- */

  .timeline {
    position: relative;
    padding: 2em 0;
    /* margin-top: 2em; */
    /* margin-bottom: 2em; */
  }

  .timeline::before {
    /* this is the vertical line */
    content: '';
    position: absolute;
    top: 0;
    left: 18px;
    height: 100%;
    width: 2px;
    background: #d7e4ed;
  }

  @media only screen and (min-width: 1170px) {
    .timeline {
      margin-top: 3em;
      margin-bottom: 3em;
    }

    .timeline::before {
      left: 50%;
      margin-left: -2px;
    }
  }

  .timeline-block {
    position: relative;
    margin: 2em 0;
  }

  .timeline-block:after {
    content: "";
    display: table;
    clear: both;
  }

  .timeline-block:first-child {
    margin-top: 0;
  }

  .timeline-block:last-child {
    margin-bottom: 0;
  }

  @media only screen and (min-width: 1170px) {
    .timeline-block {
      margin: 4em 0;
    }

    .timeline-block:first-child {
      margin-top: 0;
    }

    .timeline-block:last-child {
      margin-bottom: 0;
    }
  }

  .timeline-img {
    position: absolute;
    top: 0;
    left: .9rem;
    width: .6rem;
    height: .6rem;
    border-radius: 50%;
  }

  .timeline-img img {
    display: block;
    width: 24px;
    height: 24px;
    position: relative;
    left: 50%;
    top: 50%;
    margin-left: -12px;
    margin-top: -12px;
  }

  .timeline-img.green {
    background: #75ce66;
  }

  .timeline-img.red {
    background: #c03b44;
  }

  .timeline-img.yellow {
    background: #f0ca45;
  }

  @media only screen and (min-width: 1170px) {
    .timeline-img {
      width: 60px;
      height: 60px;
      left: 50%;
      margin-left: -30px;
      /* Force Hardware Acceleration in WebKit */
      -webkit-transform: translateZ(0);
      -webkit-backface-visibility: hidden;
    }

    .cssanimations .timeline-img.is-hidden {
      visibility: hidden;
    }

    .cssanimations .timeline-img.bounce-in {
      visibility: visible;
      -webkit-animation: bounce-1 0.6s;
      -moz-animation: bounce-1 0.6s;
      animation: bounce-1 0.6s;
    }
  }

  .timeline-content {
    position: relative;
    margin-left: 2.5rem;
    background: white;
    border-radius: 0.25em;
    padding: 1em;
    box-shadow: 0 .0625rem .375rem #d7e4ed;
  }

  .timeline-content:after {
    content: "";
    display: table;
    clear: both;
  }

  .timeline-content h2 {
    color: #303e49;
  }

  .timeline-content p, .timeline-content .read-more, .timeline-content .date {
    font-size: 13px;
    font-size: 0.8125rem;
  }

  .timeline-content .read-more, .timeline-content .date {
    display: inline-block;
  }

  .timeline-content p {
    margin: 1em 0;
    line-height: 1.6;
  }

  .timeline-content .read-more {
    float: right;
    padding: .8em 1em;
    background: #acb7c0;
    color: white;
    border-radius: 0.25em;
  }

  .no-touch .timeline-content .read-more:hover {
    background-color: #bac4cb;
  }

  a.read-more:hover {
    text-decoration: none;
    background-color: #424242;
  }

  .timeline-content .date {
    float: left;
    padding: .8em 0;
    opacity: .7;
  }

  .timeline-content::before {
    content: '';
    position: absolute;
    top: 16px;
    right: 100%;
    height: 0;
    width: 0;
    border: 7px solid transparent;
    border-right: 7px solid white;
  }

  @media only screen and (min-width: 768px) {
    .timeline-content h2 {
      font-size: 20px;
      font-size: 1.25rem;
    }

    .timeline-content p {
      font-size: 16px;
      font-size: 1rem;
    }

    .timeline-content .read-more, .timeline-content .date {
      font-size: 14px;
      font-size: 0.875rem;
    }
  }

  @media only screen and (min-width: 1170px) {
    .timeline-content {
      margin-left: 0;
      padding: 1.6em;
      width: 45%;
    }

    .timeline-content::before {
      top: 24px;
      left: 100%;
      border-color: transparent;
      border-left-color: white;
    }

    .timeline-content .read-more {
      float: left;
    }

    .timeline-content .date {
      position: absolute;
      width: 100%;
      left: 122%;
      top: 6px;
      font-size: 16px;
      font-size: 1rem;
    }

    .timeline-block:nth-child(even) .timeline-content {
      float: right;
    }

    .timeline-block:nth-child(even) .timeline-content::before {
      top: 24px;
      left: auto;
      right: 100%;
      border-color: transparent;
      border-right-color: white;
    }

    .timeline-block:nth-child(even) .timeline-content .read-more {
      float: right;
    }

    .timeline-block:nth-child(even) .timeline-content .date {
      left: auto;
      right: 122%;
      text-align: right;
    }

    .cssanimations .timeline-content.is-hidden {
      visibility: hidden;
    }

    .cssanimations .timeline-content.bounce-in {
      visibility: visible;
      -webkit-animation: bounce-2 0.6s;
      -moz-animation: bounce-2 0.6s;
      animation: bounce-2 0.6s;
    }
  }

  @media only screen and (min-width: 1170px) {
    /* inverse bounce effect on even content blocks */
    .cssanimations .timeline-block:nth-child(even) .timeline-content.bounce-in {
      -webkit-animation: bounce-2-inverse 0.6s;
      -moz-animation: bounce-2-inverse 0.6s;
      animation: bounce-2-inverse 0.6s;
    }
  }

</style>
<script>
  export default {
    name:'ui-timeline',
    props: {
      points: {
        required: true
      }
    },
    data () {
      return {
        defaultImg: ''
      }
    },
    components: {},
    methods: {
      getImg: function (imgurl) {
        var ImgObj = new Image() // 判断图片是否存在
        ImgObj.src = imgurl
        // 没有图片，则返回-1
        if (ImgObj.fileSize > 0 || (ImgObj.width > 0 && ImgObj.height > 0)) {
          return imgurl
        } else {
          return this.defaultImg
        }
      },
      changeCurrentTimePoint: function (point) {
        this.$on('currentPoint', point)
      }
    }
  }
</script>
